#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

typedef struct {
    char username[1024];
    char password[1024];
} User;

User users[100]; // 存储最多100个用户
int numUsers = 0; // 当前的用户数量

void register_user(const char* username, const char* password) {
    strncpy(users[numUsers].username, username, sizeof(users[numUsers].username) - 1);
    strncpy(users[numUsers].password, password, sizeof(users[numUsers].password) - 1);
    printf("Registered user: %s, password: %s\n", users[numUsers].username, users[numUsers].password);
    numUsers++;
}

int check_login(const char* username, const char* password) {
    for(int i = 0; i < numUsers; i++) {
        if(strcmp(users[i].username, username) == 0 && strcmp(users[i].password, password) == 0) {
            return 1; // 登录成功
        }
    }
    return 0; // 登录失败
}

void* handle_client(void* arg) {
    int conn_fd = *(int*)arg;
    FILE* file = NULL;
    int is_logged_in = 0; // 添加一个标志来表示用户是否已经登录成功

    while(1) {
        char data[200];
        ssize_t numBytesRcvd;
        ssize_t numBytesRcvd1;
        if((numBytesRcvd = recv(conn_fd, data, sizeof(data) - 1, 0)) > 0) {
            data[numBytesRcvd] = '\0';
            printf("Received data: %s\n", data);

            char type[10];
            char username[1024];
            char password[1024];
            if (sscanf(data, "%[^:]: %s password: %s", type, username, password) == 3) {
                if (strncmp(type, "register", 8) == 0) {
                    register_user(username, password);
                    printf("Registered new user: %s\n", username);
                } else if (strncmp(type, "login", 5) == 0) {
                    if(check_login(username, password)) {
                        printf("Login successful\n");
                        file = fopen("1.txt", "w"); // 打开文件，准备写入数据
                        is_logged_in = 1; // 设置登录成功标志
                    } else {
                        printf("Login failed\n");
                        break; // 登录失败，结束线程
                    }
                }
            } else if (is_logged_in && file != NULL) { // 登录成功，开始接收数据
                long long msg[3];
                while(1){
                memset(msg, 0, sizeof(msg)); // 初始化数组，数组中的值全部赋值为0
    
                numBytesRcvd1 = recv(conn_fd, msg, sizeof(msg), 0);
                if(numBytesRcvd1 < 0) {
                    perror("recv failed");
                    break;
                } else if(numBytesRcvd1 == 0) {
                    printf("Client disconnected\n");
                break;
                }

                if(sizeof(msg) > 0) {
                    printf("Received: %lld %f %f\n", msg[0],(float)msg[1],(float)msg[2]);
                    fprintf(file, "%lld %f %f\n", msg[0],(float)msg[1],(float)msg[2]);
                } else {
                    printf("Received an empty message\n");
                    }

                }
            }
        }

        if(numBytesRcvd1 < 0) {
            perror("recv failed");
            break;
        }
    }

    if (file != NULL) {
        fclose(file); // 关闭文件
    }

    close(conn_fd);
    return NULL;
}

int main(int argc, char *argv[]) {
    int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
    if(socket_fd < 0) {
        perror("socket creation failed");
        return -1;
    }

    struct sockaddr_in srvaddr;
    srvaddr.sin_family = AF_INET;
    srvaddr.sin_port = htons(55555);
    srvaddr.sin_addr.s_addr = inet_addr("192.168.64.7");

    if(bind(socket_fd, (struct sockaddr *)&srvaddr, sizeof(srvaddr)) < 0) {
        perror("bind failed");
        return -1;
    }

    if(listen(socket_fd, 4) < 0) {
        perror("listen failed");
        return -1;
    }

    printf("Waiting for client connection...\n");

    while(1) {
        int conn_fd = accept(socket_fd, NULL, NULL);
        if(conn_fd < 0) {
            perror("accept failed");
            continue;
        }

        printf("连接成功\n");

        pthread_t thread;
        pthread_create(&thread, NULL, handle_client, &conn_fd);
    }

    close(socket_fd);

    return 0;
}